﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Globalization;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using emp.Model;
using emp.ProcessFlow;
using emp.BLL;
using emp.Model.SysManage;
using emp.ProcessFlow.SysManage;
using emp.Model.crm.campaign;
using System.Text.RegularExpressions;

public partial class crm_campaign_CampaignVerify : System.Web.UI.Page
{
    
    protected int eid_view
    {
        set { ViewState["eid_view"] = value; }
        get { return Convert.ToInt32(ViewState["eid_view"]); }
    }
    protected IList source
    {
        set { ViewState["source"] = value; }
        get { return (IList)(ViewState["source"]); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int eid = 0;
            if (Request.QueryString["eid"] != null && Request.QueryString["eid"] != "")
            {
                int.TryParse(Request.QueryString["eid"].ToString(), out eid);
            }
            eid_view = eid;
            InitPage();
        }
    }
    private void InitPage()
    {
        if (eid_view > 0) //modify
        {

            CAMPAIGN model = new Campaign().GetModel(eid_view);
            label1.Text = "审核活动";
            hiduser1.Value = model.CAM_AUDIT_USER1.ToString();
            hiduser2.Value = model.CAM_AUDIT_USER2.ToString();
            txtBeizhu.Text = model.CAM_AUDIT_NOTES;
            CAM_CODE.Text = model.CAM_CODE;
            CAM_NAME.Text = model.CAM_NAME;
            CAM_BEGINTIME.Text = model.CAM_BEGIN_DATE.ToShortDateString();
            CAM_ENDTIME.Text = model.CAM_END_DATE.ToShortDateString();
            CAM_TARGET_CUSTOMERS.Text = model.CAM_TARGET_CUSTOMERS;
            CAM_SMS_CONTENTS.Text = model.CAM_SMS_CONTENTS;
            CAM_DESCRIPTION.Text = model.CAM_DESCRIPTION;
            CAM_TEST_CELLPHONES.Text = model.CAM_TEST_CELLPHONES;

            if (model.CAM_SEND_TIME.HasValue)
            {
                CAM_SEND_TIME.Text = model.CAM_SEND_TIME.Value.ToShortDateString();
                hour.Text = model.CAM_SEND_TIME.Value.Hour.ToString();
                min.Text = model.CAM_SEND_TIME.Value.Minute.ToString();
                second.Text = model.CAM_SEND_TIME.Value.Second.ToString();
            }

            

            //LIS_SUMMARY.Text = "";//model中无此值
            //LIS_STATUS.SelectedValue = model.CAM_STATUS;

            //if (PubFun.regionRole.IndexOf(UserController.GetUserSession().Suser_type_id.ToString()) != -1)
            //{
            //    LIS_STATUS.Items.Add(new ListItem(PubFun.CAM_STATUS.区域经理审批未通过.ToString(), ((int)PubFun.CAM_STATUS.区域经理审批未通过).ToString()));
            //    LIS_STATUS.Items.Add(new ListItem(PubFun.CAM_STATUS.区域经理审批通过.ToString(), ((int)PubFun.CAM_STATUS.区域经理审批通过).ToString()));
            //}
            //else if (PubFun.marketRole.IndexOf(UserController.GetUserSession().Suser_type_id.ToString()) != -1)
            //{
            //    LIS_STATUS.Items.Add(new ListItem(PubFun.CAM_STATUS.市场部审批未通过.ToString(), ((int)PubFun.CAM_STATUS.市场部审批未通过).ToString()));
            //    LIS_STATUS.Items.Add(new ListItem(PubFun.CAM_STATUS.市场部审批通过.ToString(), ((int)PubFun.CAM_STATUS.市场部审批通过).ToString()));
            //}
            //ListItem lItem = LIS_STATUS.Items.FindByValue(model.CAM_STATUS.Trim());
            //if (lItem != null)
            //    lItem.Selected = true;

            //REMARKS.Text = "as";
            // MSMTYPE.SelectedValue = "短信";
            //REMARKS.Text=model.CAM_AUDIT_NOTES;
            source = new Lists().getList(eid_view, UserController.GetUserSession().UserID);
            NAMELIST.DataSource = source;
            NAMELIST.DataBind();
            StatisticalNumber();

        }
        else
        {
            Response.Redirect("CampaignVerifyList.aspx");
        }

    }

    private void StatisticalNumber()
    {
        LIS_SUMMARY.Text = GetSelect().Length > 0
                              ? (new Campaign().Insert_CAMPAIGN_LISTS_And_CAMPAIGN_Send(GetSelect(),
                                                                                       UserController.GetUserSession().
                                                                                           UserID).ToString(CultureInfo.InvariantCulture))
                              : "0";
    }


    //int uid = UserController.GetUserSession().UserID;

    //foreach (DataColumn col in NAMELIST.Columns)
    //{

    //}
    //foreach (DataRow row in NAMELIST.Rows)
    //{

    //}
    //Campaign bll = new Campaign();
    //CAMPAIGN model = new CAMPAIGN();

    //bll.Add(model);

    //bll.Update(model);

    protected void save_CAMPAIGN(bool b)
    {
        CAMPAIGN model = new CAMPAIGN();

        //model.CAM_STATUS = LIS_STATUS.SelectedValue;
        model.CAM_AUDIT_NOTES = txtBeizhu.Text;
        if (PubFun.regionRole.IndexOf(UserController.GetUserSession().Suser_type_id.ToString()) != -1)
        {
            model.CAM_AUDIT_USER1 = UserController.GetUserSession().UserID;
            if (b)
                model.CAM_STATUS = ((int)PubFun.CAM_STATUS.区域经理审批通过).ToString();
            else
                model.CAM_STATUS = ((int)PubFun.CAM_STATUS.区域经理审批未通过).ToString();
        }
        else
        {
            model.CAM_AUDIT_USER1 = int.Parse(hiduser1.Value);
            if (b)
                model.CAM_STATUS = ((int)PubFun.CAM_STATUS.市场部审批通过).ToString();
            else
                model.CAM_STATUS = ((int)PubFun.CAM_STATUS.市场部审批未通过).ToString();
        }
        if (PubFun.marketRole.IndexOf(UserController.GetUserSession().Suser_type_id.ToString()) != -1)
            model.CAM_AUDIT_USER2 = UserController.GetUserSession().UserID;
        else
            model.CAM_AUDIT_USER2 = int.Parse(hiduser2.Value);
        if (eid_view > 0)
        {
            model.CAM_UID = eid_view;
            if (new Campaign().UpdateVerify(model))
            {
                PubFun.Show(this, "修改成功", "CampaignVerifyList.aspx");

            }
            else
            {
                PubFun.Show(this, "修改失败");
            }

        }

    }
    protected void sum_Click(object sender, EventArgs e)
    {
        string s = GetSelect();
        if (s == "")
        {
            PubFun.Show(this, "先选择名单，再计算！");
            return;
        }

        int result = new Campaign().Insert_CAMPAIGN_LISTS_And_CAMPAIGN_Send(GetSelect(), UserController.GetUserSession().UserID);
        //LIS_SUMMARY.Text = result.ToString();
    }

    private string GetSelect()
    {
        string liststr = string.Empty;
        foreach (GridViewRow row in NAMELIST.Rows)
        {
            Label lb = (Label)row.FindControl("lab_id");
            if (lb != null)
            {
                liststr += lb.Text + ",";
            }
        }
        if (liststr.EndsWith(","))
        {
            liststr = liststr.Substring(0, liststr.Length - 1);
        }
        return liststr;

    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        string selval = Request.Params["hidsel"];
        if (selval != null && selval != "")
        {
            emp.BLL.Lists BLL = new Lists();

            if (selval.EndsWith(","))
            {
                selval = selval.Substring(0, selval.Length - 1);
            }
            //IList datalist = list.getCAMPAIGN();
            source = BLL.getlist(selval);
            NAMELIST.DataSource = source;
            NAMELIST.DataBind();
            
        }
    }
    protected void DeleteByUID(object sender, System.EventArgs e)
    {
        int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
        string uid = ((LinkButton)sender).CommandArgument;
        ArrayList arr = (ArrayList)source;
        arr.RemoveAt(row);
        NAMELIST.DataSource = arr;
        NAMELIST.DataBind();
    }

    protected void NAMELIST_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //((GridView)sender).DeleteRow(Convert.ToInt32(e.CommandArgument));

    }
    protected void NAMELIST_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
        //NAMELIST.DeleteRow(row);

    }

    private bool Validation(CAMPAIGN model)
    {
        string error = string.Empty;
        if (!(model.CAM_CODE.Length > 0))
        {
            error += "活动代码不能为空\\n";
        }
        if (model.CAM_NAME.Length <= 0)
        {
            error += "活动名称不能为空\\n";
        }
        DateTime begin, end, send;
        if (!DateTime.TryParse(model.CAM_BEGIN_DATE.ToString(), out begin))
        {
            error += "活动的开始时间不能为空\\n";
        }
        if (!DateTime.TryParse(model.CAM_END_DATE.ToString(), out end))
        {
            error += "活动的结束时间不能为空\\n";
        }
        if (model.CAM_TARGET_CUSTOMERS.Length <= 0)
        {
            error += "目标人群不能为空\\n";
        }
        if (model.CAM_SMS_CONTENTS.Length <= 0)
        {
            error += "短信内容不能为空\\n";
        }
        if (model.CAM_TEST_CELLPHONES.Length <= 0)
        {
            error += "测试号码不能为空\\n";
        }
        if (!CheckMobile(model.CAM_TEST_CELLPHONES))
        {
            error += "测试号码输入错误\\n";
        }
        if (!DateTime.TryParse(CAM_SEND_TIME.Text, out send))
        {
            error += "发送时间输入错误\\n";
        }
        if (error.Length > 0)
        {
            PubFun.Show(this, error);
            return false;
        }
        return true;

    }




    /// <summary>
    /// 验证多个手机号以;拆分 1.位数验证（11位） 2.规则验证  By:Cp When：2012-06-27
    /// </summary>
    /// <param name="mobile">手机号字符串</param>
    /// <returns> 返回bool型结果 ture: 是手机号 flase:不是手机号</returns>
    private bool CheckMobile(string mobile)
    {
        //mobile = "1;12345678921;18610497160";  //多个手机号
        //mobile = "1861049716";   //长度不够
        //mobile = "12345678921";  //不是手机号
        //mobile = "aaaaaaaaaaa";  //不是手机号
        //mobile = "18610497160";  //正确的手机号
        bool flag = false;
        string[] strArr = mobile.Split(';');
        if (strArr.Length >= 0)
        {
            foreach (string str in strArr)
            {
                if (str.Trim().Length != 11)
                {//验证长度
                    // flag = false;
                    return false;
                }
                else
                {
                    Regex ReMobile = new Regex(@"^1[358]\d{9}$");
                    if (!ReMobile.IsMatch(str.Trim()))
                    {//验证是否是手机号
                        // flag = false;
                        return false;
                    }
                    //else
                    //{
                    //    flag = true;
                    //}
                }
            }
        }
        else
        {
            // flag = false;
            return false;
        }
        return true;
    }

    /// <summary>
    /// 通过事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void tongguo_Click(object sender, EventArgs e)
    {
        save_CAMPAIGN(true);
    }
    /// <summary>
    /// 未通过事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void nottongguo_Click(object sender, EventArgs e)
    {
        save_CAMPAIGN(false);
    }
}
